//
//  cardtest2.c
//  tests adventurerCard() with random inputs
//  
//  Created by Preston Skaggs on 1/25/14.
//
//

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "interface.h"
#include <math.h>
#include <assert.h>
#include <time.h>

#define CARD_FUNCTION 0
#define NUM_TESTS 5000
/***************************
 Function Prototypes
 **************************/
double randGen();
void initPlayerCards(int player, int handSize, int discardSize, int deckSize, int *kingdom, struct gameState *state);
/***************************
 Helper Functions
 **************************/
/*------------------
 RNG 0-1
 ------------------*/
double randGen(){
    return  (double)rand()/(double)RAND_MAX;
}
/*------------------
 Player's hand initilize
 ------------------*/
void initPlayerCards(int player, int handSize, int discardSize, int deckSize, int *kingdom, struct gameState *state){
    enum CARD card = adventurer;
    int kingdomCard, i;
//This function initializes the players hand, deck, and discard with 50% treasure cards and 50% kingdom cards
//set players hand
        state->hand[player][0] = card; // guarantee first card is adventurer
        for(i=1; i<handSize-1; i++){
            if(rand() % 2 == 0){
                if(rand()%3 == 0)
                    state->hand[player][i] = copper;
                else if(rand()%3 ==1)
                    state->hand[player][i] = silver;
                    
                else
                    state->hand[player][i] = gold;
            }
            else{
                kingdomCard = rand()%10;
                state->hand[player][i] = kingdom[kingdomCard];
            }
        }
    
//set players deck
        for(i=0; i<deckSize; i++){
            if(rand() % 2 == 0){
                if(rand()%3 == 0)
                    state->deck[player][i] = copper;
                else if(rand()%3 ==1)
                    state->deck[player][i] = silver;
                
                else
                    state->deck[player][i] = gold;
            }
            else{
                kingdomCard = rand()%10;
                state->deck[player][i] = kingdom[kingdomCard];
            }
        }

//set discard
    for(i=0; i<deckSize; i++){
        if(rand() % 2 == 0){
            if(rand()%3 == 0)
                state->discard[player][i] = copper;
            else if(rand()%3 ==1)
                state->discard[player][i] = silver;
        
            else
                state->discard[player][i] = gold;
        }
        else{
            kingdomCard = rand()%10;
            state->discard[player][i] = kingdom[kingdomCard];
        }
    }


}
/***************************
 Main Function
 **************************/
int main(){
    srand(time(NULL));
    struct gameState *state = malloc(sizeof(struct gameState));
    assert(state != 0);
    int numPlayers, drawnTreasure, choice1, choice2, choice3, handPos, n, init, test, player;
    enum CARD first = adventurer;
    enum CARD last = treasure_map;
    int bonus = 0;
    int randomSeed = 300;
    int kingdomCards[NUM_K_CARDS];
    
    
    if(CARD_FUNCTION == 0){
        for(n=0; n<=NUM_TESTS; n++){
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(randGen()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
                //printf("Initialization failed  numPlayers = %d\n", numPlayers);
                continue;
            }
            drawnTreasure = floor(randGen()*6);//I'm not sure why I picked 6
            player = floor(randGen()*numPlayers);
            state->whoseTurn = player;
            state->handCount[player] = floor(randGen()*MAX_HAND)+1;
            if(MAX_DECK-state->handCount[player] < 0) state->deckCount[player] = 0;
            else state->deckCount[player] = floor(randGen()*(MAX_DECK-(state->handCount[player])));
            if(MAX_DECK-(state->handCount[player])-(state->deckCount[player]) < 0) state->discardCount[player] =0;
            else state->discardCount[player] = floor(randGen()*(MAX_DECK-(state->handCount[player])-(state->deckCount[player])));
            initPlayerCards(player, state->handCount[player], state->discardCount[player], state->deckCount[player], kingdomCards, state);
            //printf("test %d  drawnTreasure = %d player = %d deckCount %d\n ", n, drawnTreasure, player, state->deckCount[player]);
            test = adventurerCard(drawnTreasure, player, state);
            
        }
        printf("Tests Finished\n");
        
    }
    else{
        for(n=0; n<=NUM_TESTS; n++){
   //This test isn't very effective because drawnTreasure is always = 0 when adventurerCard() is called from cardEffect()
    //Also, the random generation of players isn't working.
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(randGen()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
               // printf("Initialization failed  numPlayers = %d\n", numPlayers);
                continue;
            }
            drawnTreasure = floor(randGen()*6);
            player = floor(randGen()*numPlayers);
            state->whoseTurn = player;
            state->handCount[player] = floor(randGen()*MAX_HAND)+1;
            if(MAX_DECK-state->handCount[player] < 0) state->deckCount[player] = 0;
            else state->deckCount[player] = floor(randGen()*(MAX_DECK-(state->handCount[player])));
            if(MAX_DECK-(state->handCount[player])-(state->deckCount[player]) < 0) state->discardCount[player] =0;
            else state->discardCount[player] = floor(randGen()*(MAX_DECK-(state->handCount[player])-(state->deckCount[player])));
            initPlayerCards(player, state->handCount[player], state->discardCount[player], state->deckCount[player], kingdomCards, state);
            handPos = (randGen()*state->handCount[player]);
            choice1 = (randGen() *(last-first))+first;
            choice2 = (randGen() *(last-first))+first;
            choice3 = (randGen() *(last-first))+first;
            cardEffect(first, choice1, choice2, choice3, state, handPos, &bonus);
        }
    }
    printf("Tests Finished \n");
    return 0;
}